package common.main.tianYanCha;

import com.google.gson.Gson;
import common.http.HtmlInfo;
import common.http.SimpleHttp;
import common.main.google.NotFoundException;
import common.utils.DomTree;
import org.apache.log4j.Logger;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.NodeList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TianYanChaMain {


    static Logger logger = Logger.getLogger(TianYanChaMain.class);
    static HtmlInfo html = new HtmlInfo();
    static SimpleHttp http = new SimpleHttp();
    static String ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36";

    public static void main(String[] args) throws InterruptedException {

        html.setEncode("UTF-8");
        html.setUa(ua);
        //登陆操作
        login();

        TianYanChaConfig.init();
        String url = null;
        if(TianYanChaConfig.entranceUrl == null){
            url = "https://www.tianyancha.com/brandsearch/%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD?pn=1";
        }else{
            url = TianYanChaConfig.entranceUrl;
        }
        int retry = 0;
        List<TianYanChaData> companyList = new ArrayList<>();

        while (url != null && retry < 2){
            companyList.clear();
            logger.info("当前列表url：" + url);
            String retryUrl = url;
            html.setOrignUrl(url);
            http.simpleGet(html);

            DocumentFragment companyListNode = DomTree.getNode(html.getContent(),html.getEncode());
            boolean success = extractCompanyInfo(companyList,companyListNode);
            if(success){
                retry = 0;
                extractCompanyName(companyList,companyListNode);
                extractCompanyFinancing(companyList,companyListNode);
                extractCompanyFoundingTime(companyList,companyListNode);
                extractCompanyPlace(companyList,companyListNode);
                extractCompanyProductIntroduction(companyList,companyListNode);
                logger.info("保存数据到数据库");
                TianYanChaSaveDataToSql.saveDataToSql(companyList);
                logger.info("数据保存完成，15s后开始获取下一页信息");
                Thread.sleep(15 * 1000);
                String [] urlAndPage = url.split("=");
                url = urlAndPage[0] + "=" + (Integer.parseInt(urlAndPage[1]) + 1);

            }else{
                logger.info("获取信息失败，15s后重试");
                Thread.sleep(15 * 1000);
                html = new HtmlInfo();
                html.setEncode("UTF-8");
                html.setUa(ua);
                url = retryUrl;
                retry += 1;
            }
        }
    }


    public static void login(){
        //登陆操作
        html.setOrignUrl("https://www.tianyancha.com/cd/login.json");
        Map userInfo = new HashMap();
        userInfo.put("autoLogin","False");
        userInfo.put("cdpassword","10e3073ef6e522754b9070bb894e579b");
        userInfo.put("loginway","PL");
        userInfo.put("mobile","17660615273");
        http.simplePostWithJson(html,userInfo);
        Gson gson = new Gson();
        Map data = (Map)(gson.fromJson(html.getContent(),Map.class).get("data"));
        String token = (String) data.get("token");
        html.setCookie(html.getCookie() + "; " + "auth_token=" + token);
    }

    public static boolean extractCompanyInfo(List<TianYanChaData> companyList,DocumentFragment companyListNode){
        NodeList companyInfoNodeList = DomTree.commonList(TianYanChaXpath.companyInfo,companyListNode);
        if(companyInfoNodeList != null){
            for(int i = 0;i < companyInfoNodeList.getLength();i++){
                String companyInfo  = companyInfoNodeList.item(i).getTextContent();
                TianYanChaData company = new TianYanChaData();
                company.setCompanyInfo(companyInfo);
                companyList.add(company);
            }
            return true;
        }else{
            return false;
        }
    }

    public static void extractCompanyName(List<TianYanChaData> companyList,DocumentFragment companyListNode) throws InterruptedException {
        logger.info("开始获取公司名称");
        NodeList companyNameNodeList = DomTree.commonList(TianYanChaXpath.companyName,companyListNode);
        if(companyNameNodeList != null){
            for(int i = 0;i < companyNameNodeList.getLength();i++){
                String companyName = companyNameNodeList.item(i).getTextContent();
                TianYanChaData company = companyList.get(i);
                company.setCompany_name(companyName);
                //获取公司标签以及网址
                String keywordPageUrl = companyNameNodeList.item(i).getAttributes().getNamedItem("href").getTextContent();
                logger.info("等待15s获取<"+ companyName + ">公司关键字标签");
                Thread.sleep(15 * 1000);
                html.setOrignUrl(keywordPageUrl);
                try{
                    http.simpleGet(html);
                }catch (NotFoundException e){
                    continue;
                }
                DocumentFragment companyKeywordNode = DomTree.getNode(html.getContent(),html.getEncode());
                NodeList companyKeywordNodeList = DomTree.commonList(TianYanChaXpath.companyKeyword,companyKeywordNode);
                if(companyKeywordNodeList != null){
                    String keyword = "";
                    for(int j = 0;j < companyKeywordNodeList.getLength();j++){
                        keyword = keyword + companyKeywordNodeList.item(j).getTextContent() + ";";
                    }
                    company.setCompany_keyword(keyword);
                }
                NodeList parentCompanyNodeList = DomTree.commonList(TianYanChaXpath.parentCompany,companyKeywordNode);
                String parentCompanyUrl = null;
                if(parentCompanyNodeList != null && parentCompanyNodeList.getLength() > 0){
                    parentCompanyUrl = parentCompanyNodeList.item(0).getTextContent();
                }
                if(parentCompanyUrl != null){
                    logger.info("等待15s获取<"+ companyName + ">公司网站信息");
                    Thread.sleep(15 * 1000);
                    html.setOrignUrl(parentCompanyUrl);
                    http.simpleGet(html);
                    DocumentFragment companyUrlNode = DomTree.getNode(html.getContent(),html.getEncode());
                    NodeList companyUrlNodeList = DomTree.commonList(TianYanChaXpath.companyURL,companyUrlNode);

                    if(companyUrlNodeList != null && companyUrlNodeList.getLength() > 0){
                        company.setCompany_url(companyUrlNodeList.item(0).getTextContent());
                    }
                }
            }
        }
    }
    public static void extractCompanyFinancing(List<TianYanChaData> companyList,DocumentFragment companyListNode){
        logger.info("开始获取公司融资信息");
        NodeList companyFinancingNodeList = DomTree.commonList(TianYanChaXpath.companyFinancing,companyListNode);
        if(companyFinancingNodeList != null){
            for(int i = 0;i < companyFinancingNodeList.getLength();i++){
                String companyFinancing = companyFinancingNodeList.item(i).getTextContent();
                TianYanChaData company = companyList.get(i);
                company.setCompany_financing(companyFinancing);
            }
        }
    }
    public static void extractCompanyFoundingTime(List<TianYanChaData> companyList,DocumentFragment companyListNode){
        logger.info("开始获取公司创建时间");
        NodeList companyFoundingTimeNodeList = DomTree.commonList(TianYanChaXpath.companyFoundingTime,companyListNode);
        if(companyFoundingTimeNodeList != null){
            for(int i = 0;i < companyFoundingTimeNodeList.getLength();i++){
                String companyFoundingTime = companyFoundingTimeNodeList.item(i).getTextContent();
                TianYanChaData company = companyList.get(i);
                company.setCompany_founding_time(companyFoundingTime);
            }
        }
    }
    public static void extractCompanyPlace(List<TianYanChaData> companyList,DocumentFragment companyListNode){
        logger.info("开始获取公司所属地");
        NodeList companyPlaceNodeList = DomTree.commonList(TianYanChaXpath.companyPlace,companyListNode);
        if(companyPlaceNodeList != null){
            for(int i = 0;i < companyPlaceNodeList.getLength();i++){
                String companyPlace = companyPlaceNodeList.item(i).getTextContent();
                TianYanChaData company = companyList.get(i);
                company.setCompany_place(companyPlace);
            }
        }
    }
    public static void extractCompanyProductIntroduction(List<TianYanChaData> companyList,DocumentFragment companyListNode){
        logger.info("开始获取公司产品介绍");
        NodeList companyProductIntroductionNodeList = DomTree.commonList(TianYanChaXpath.companyProductIntroduction,companyListNode);
        if(companyProductIntroductionNodeList != null){
            for(int i = 0;i < companyProductIntroductionNodeList.getLength();i++){
                String companyProductIntroduction = companyProductIntroductionNodeList.item(i).getTextContent();
                TianYanChaData company = companyList.get(i);
                company.setCompany_product_introduction(companyProductIntroduction);
            }
        }
    }
}
